import Vue from 'vue'
import Router from 'vue-router'

//layout
import Home from '@/components/views/layout/Home' //同步加载
const Index = () => import('@/components/views/Index')  //异步加载



Vue.use(Router)

import activityListRoute from '@/router/activity_list_route'
import activityBannerRoute from '@/router/activity_banner'
import activitySettingRoute from '@/router/activity_setting'
import activityClassifyRoute from '@/router/activity_classify'


const routes = [
    {
        path: '/',
        component: Home,
        children: [
            {
                path: '',
                redirect: '/activity/list'
            },
            {
                path: 'index',
                name: 'index',
                component: Index
            },
            activityListRoute,
            activityBannerRoute,
            activityClassifyRoute,
            activitySettingRoute
        ]
    }
]

const router = new Router({
    routes
})

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}
import store from '@/store'
import {Message} from 'element-ui';
import { MessageBox } from 'element-ui';
router.beforeEach((to, from, next) => {
     const keyAdmin = getQueryString('keyAdmin');

     if (keyAdmin) {
         store.state.keyAdmin = keyAdmin;
         Vue.http.get('marketweb/index/vallogin', {
             params: {
                 keyAdmin: keyAdmin,
                 childid: getQueryString('childid')
             }
         }).then(response => {
             if (response.ok) {
                 const result = response.body;
                 if (result.code == 0) {
                     var loginStatus = result.code ;
                     var describe = result.describe;
                     if (loginStatus==0) {
                         store.state.describe = describe;
                          Vue.http.post('marketweb/index/init',{
                                keyAdmin:keyAdmin
                            }).then(response1=>{
                                if(response1.body.code == 200){
                                    next();
                                }else{
                                    //self.isShowModal = true
                                    Message({
                                     message: response.body.msg,
                                     type: 'error'
                                 });
                                }
                            },response=>{

                            })
                           next()
                        
                     } else {
                         Message({
                             message: '请先登录，准备跳转到登录页面。。。',
                             type: 'error',
                             duration: 3000,
                             onClose: function () {
                                 //跳转到登录页面
                                 window.location.href = 'http://vip.rtmap.com/user/login';
                             }
                         });
                     }
                 } else {
                    MessageBox.alert(result.msg, '提示', {
                          confirmButtonText: '确定',
                          callback: action => {
                            Message({
                              type: 'info',
                              message: '返回登录页面',
                              duration: 800,
                              onClose: function () {
                                 window.location.href = 'http://vip.rtmap.com/user/login';
                             }
                            });
                        }
                    });
                 }

             } else {
                 Message.error('获取登录状态错误');
             }
         }, response => {
             Message.error('获取登录状态失败');
         });
     } else {
         Message.error('缺少keyAdmin参数');
     }

})

export default router
